package com.yunhui.controller;


import com.yunhui.entity.RouteJb;
import com.yunhui.entity.RouteXx;
import com.yunhui.service.RouteJbService;
import com.yunhui.service.RouteXxService;
import com.yunhui.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  行程的基本信息前端控制层
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Controller
@RequestMapping("/routeXx")
public class RouteXxController {

    @Autowired
    RouteXxService routeXxService;

    @Autowired
    RouteJbService routeJbService;

    /**
     * 跳转至详情
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/toxxlist")
    public String toXxList(
            Model model,Integer id
    ){
        RouteJb routeJb=null;
        routeJb=routeJbService.selectByIds(id);
        List<RouteXx> routeXx=null;
        routeXx =routeXxService.getListByJbId(id);
        model.addAttribute("routeJb",routeJb);
        model.addAttribute("list",routeXx);
        return "xiangqing";
    }

    /**
     * 跳转至详情管理页
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("shangxxlist")
    public String xxList(
            Model model,String id,String title,String index,String biao
    ){
        if("4".equals(biao)){
            model.addAttribute("gigu","删除成功！");
        }
        if("5".equals(biao)){
            model.addAttribute("gigu","删除失败！");
        }
        if("1".equals(biao)){
            model.addAttribute("gigu","添加成功！");
        }
        if("x".equals(biao)){
            model.addAttribute("gigu","修改成功！");
        }
        if("xc".equals(biao)){
            model.addAttribute("gigu","修改失败！");
        }
        int ind=0;
        if(index!=null&&!"".equals(index)){
            ind=Integer.parseInt(index);
        }
        if(ind ==0){
            ind=1;
        }
        String ti=null;
        Integer jbId=null;
        if(!"null".equals(title)&&title!=null&&!"".equals(title))ti=title;
        if(!"null".equals(id)&&id!=null&&!"".equals(id))jbId=Integer.valueOf(id);
        PageSupport page=new PageSupport();
        page.setCurrentPageNo(ind);
        page.setPageSize(5);
        page.setTotalCount(routeXxService.getCount(jbId,ti));

        List<RouteXx> routeXx=null;
        routeXx =routeXxService.getListByIdTitlePage(jbId,ti,page);
        model.addAttribute("list",routeXx);
        model.addAttribute("title",ti);
        model.addAttribute("jbId",id);
        model.addAttribute("page",page);
        return "shang/shangXx-list";
    }

    /**
     *跳转至修改
     * @return
     */
    @RequestMapping("tosetxx")
    public String tuset(
        Integer id,Model model
    ){
        RouteXx routeXx=routeXxService.selectById(id);
        model.addAttribute("routeXx",routeXx);

        return "shang/routexxset";

    }

    /**
     * 修改操作
     * @param routeXx
     * @return
     */
    @RequestMapping("updateXx")
    public String updateXx (
            RouteXx routeXx
    ){
        int num=0;
        num=routeXxService.updateXx(routeXx);

        if(num>0){
            return "redirect:/routeXx/shangxxlist?biao=x&id="+routeXx.getJbId();
        }
        return "redirect:/routeXx/shangxxlist?biao=xc&id="+routeXx.getJbId();
    }



    /**
     * 添加详细
     * @param routeXx
     * @param uploadFile
     * @param model
     * @return
     */
    @RequestMapping("/routexxadd")
    public String add(RouteXx routeXx,
                      MultipartFile[] uploadFile,
                      Model model){
        String []names=new RouteJbController().getName(uploadFile);
        if(names==null){
            model.addAttribute("gigu","文件上传出错！");
            return "houtai/admin-add";
        }
        routeXx.setImgName1(names[0]);
        routeXx.setImgName2(names[1]);
        routeXx.setImgName3(names[2]);
        routeXx.setImgName4(names[3]);
        int i=0;
        try {
            i=routeXxService.insect(routeXx);
        }catch (Exception e){
            e.printStackTrace();
            model.addAttribute("gigu","添加失败！");
            return "shang/routexxadd";
        }
        if(i>0){
            return "redirect:/routeXx/shangxxlist?biao=1&id="+routeXx.getJbId();
        }else{
            model.addAttribute("gigu","添加失败！");
            return "shang/routexxadd";
        }
    }



    //跳转至添加页
    @RequestMapping("/toadd")
    public String toRouxxAdd(
            Model model,Integer id
    ){
       model.addAttribute("jbId",id);
        return"shang/routexxadd";
    }

    /**
     *根据id删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String delete(
            Integer id
    ){
        int num=routeXxService.deleteById(id);
        if(num>0){
            return "redirect:/routeXx/shangxxlist?biao=4";
        }
        return "redirect:/routeXx/shangxxlist?biao=5";
    }




}

